package handler

import (
	"Walker/app/utils/response"
	"Walker/global"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
)

func Logger() gin.HandlerFunc {
	//开启生成模式捕捉错误写入日志文件中
	return gin.RecoveryWithWriter(&LogHandler{}, func(c *gin.Context, err interface{}) {
		if global.ConfigValue.GetBool("AppDebug") == true {
			response.Error(c, fmt.Sprintf("%s", err))
		} else {
			response.Error(c, fmt.Sprintf("程序异常输出"))
		}
	})
}

type LogHandler struct{}

func (receiver *LogHandler) Write(b []byte) (n int, err error) {
	errStr := string(b)
	err = errors.New(errStr)
	global.InitLogger.Error(errStr)
	return len(errStr), err
}
